package gui;

import gameEngine.Game;
import gameEngine.SGFGame;
import gameEngine.SGFGame.NoSuchVariationException;
import gameEngine.SGFGame.WrongSGFFileException;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * @author Igor Adamski
 *
 */
public class SGFControlPanel extends JPanel {
	private static final long serialVersionUID = 7679125994325066286L;
	private class VariationListener implements MouseListener, ActionListener {
		private final int i;
		public VariationListener(int i) {
			this.i = i;
		}
		@Override
		public void mouseClicked(MouseEvent arg0) {}
		@Override
		public void mouseEntered(MouseEvent arg0) {
			try {
				drawer.update(sgfGame.getVariationBoard(i));
			} catch (NoSuchVariationException e) {	}
		}
		@Override
		public void mouseExited(MouseEvent arg0) {
			drawer.update(sgfGame.getBoard());
		}
		@Override
		public void mousePressed(MouseEvent arg0) {}
		@Override
		public void mouseReleased(MouseEvent arg0) {}
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				if(i == -1)
					sgfGame.goBack();
				else
					sgfGame.goVariation(i);
			} catch (NoSuchVariationException e1) {}
			drawer.update(sgfGame.getBoard());
			commentPanel.setComment(sgfGame.getComment());
			variationsPanel.paint();
		}
	}
	private class VariationsPanel extends JPanel {
		private static final long serialVersionUID = 2736123132968566362L;
		public VariationsPanel() {
			LayoutManager layout;
			layout = new FlowLayout(FlowLayout.LEFT);
			setLayout(layout);
		}
		public void paint() {
			while(getComponentCount() != 0)
			{	
				remove(0);
				validate();
			}
			int number = sgfGame.getVariationsNumber();
			for(int i=-1;i<number;++i)
			{
				JButton var = new JButton();
				if(i == -1)
					var.setText("wróć");
				else
					var.setText("" + Character.valueOf((char) ('a' + i)));
				VariationListener tmpi = new VariationListener(i);
				var.addMouseListener(tmpi);
				var.addActionListener(tmpi);
				add(var);
			}
			validate();
		}
	}
	
	private class CommentPanel extends JPanel {
		private static final long serialVersionUID = 8314341320237848486L;
		private JTextArea comment;
		private JScrollPane scrollPane;
		public CommentPanel() {
			setLayout(new GridLayout(2, 1));
			add(new JLabel("Komentarz:"));
			comment = new JTextArea(8, 35);
			comment.setEditable(false);
			scrollPane = new JScrollPane(comment);
			add(scrollPane);
		}
		public void setComment(String t) {
			comment.setText(t);
			comment.setCaretPosition(0);
		}
	}
	
	private class ConstrolsPanel extends JPanel {
		private static final long serialVersionUID = -6648861434887273153L;
		public ConstrolsPanel(VariationsPanel vPanel, CommentPanel cPanel) {
			setLayout(new GridLayout(2, 1));
			add(vPanel);
			add(cPanel);
		}
	}
	
	private SGFGame sgfGame;
	private BoardDrawer drawer;
	private VariationsPanel variationsPanel;
	private CommentPanel commentPanel;
	public SGFControlPanel() {
		drawer = new BoardDrawer(new Dimension(500, 500));
		drawer.enablePassButton(false);
		drawer.clean();
		
		add(drawer);

		variationsPanel = new VariationsPanel();
		commentPanel = new CommentPanel();
		
		add(new ConstrolsPanel(variationsPanel, commentPanel));
	}
	
	public void loadSGFFile(File file) throws IOException, WrongSGFFileException {
		FileInputStream input = new FileInputStream(file);
		byte[] buf = new byte[input.available()];
		input.read(buf, 0, buf.length);
		sgfGame = Game.loadSGF(new String(buf));
		drawer.update(sgfGame.getBoard());
		variationsPanel.paint();
	}
}
